package com.honghu.villagepost.activity.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.honghu.villagepost.R
import com.honghu.villagepost.activity.GoodsDetailsActivity
import com.honghu.villagepost.adapter.GoodsAdapter
import com.honghu.villagepost.api.ApiService
import com.honghu.villagepost.base.LazyFragment
import com.honghu.villagepost.callback.DialogCallback
import com.honghu.villagepost.callback.LzyResponse
import com.honghu.villagepost.constant.Constants
import com.honghu.villagepost.entity.Goods
import com.honghu.villagepost.util.DensityUtil
import com.honghu.villagepost.widget.decoration.GridSpacingItemDecoration
import com.lzy.okgo.model.Response
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.include_refresh_layout.*


/**
 * @author: mqwang
 * @create: 1/26/21 10:19 AM
 * @Description: 逛城市fragment
 */
class GoodsFragment : LazyFragment() {

    private var cid: Int = 0
    private var current: Int = 1
    private var shopId: Int = 0

    private val goodsAdapter : GoodsAdapter by lazy {
        GoodsAdapter()
    }

    companion object {
        fun newInstance(shopId: Int, cid: Int): GoodsFragment {
            val fragment = GoodsFragment()
            val bundle = Bundle()
            bundle.putInt(Constants.SHOP_ID, shopId)
            bundle.putInt(Constants.CID, cid)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? = inflater.inflate(R.layout.include_refresh_layout, container, false)


    override fun lazyInit() {
        shopId = arguments!!.getInt(Constants.SHOP_ID)
        cid = arguments!!.getInt(Constants.CID)

        recyclerView.layoutManager = GridLayoutManager(activity, 2)
        recyclerView.addItemDecoration(GridSpacingItemDecoration(2, DensityUtil.dp2px(8f), true))
        recyclerView.adapter = goodsAdapter

        goodsAdapter.setOnItemClickListener { adapter, view, position ->
            var goods = adapter.getItem(position) as Goods
            GoodsDetailsActivity.self(activity, goods.id)
        }

        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                current = 1
                getData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                getData()
            }
        })

        refreshLayout.autoRefresh()
    }

    fun getData(){
        ApiService.goods(this, shopId, cid, "", current, object : DialogCallback<LzyResponse<Goods>>(){
            override fun onSuccess(response: Response<LzyResponse<Goods>>?) {
                if (current == 1){
                    goodsAdapter.setNewData(response!!.body().data.records)
                }else {
                    goodsAdapter.addData(response!!.body().data.records)
                }
                current ++
                finishRefresh(refreshLayout)

            }
        })
    }

}